package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.ModuleRole;
import com.sjlh.centurylianhua.mapper.ModuleRoleMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/moduleRole")
@CrossOrigin
public class ModuleRoleController {
    @Resource
    private ModuleRoleMapper mrm;

    /**
     * 根据角色id查询已拥有权限的id
     */
    @RequestMapping("/getModuleByRole")
    public ResultData getModuleByRole(@RequestBody ModuleRole moduleRole){
        QueryWrapper<ModuleRole> wrapper=new QueryWrapper<>();
        wrapper.eq("roleId",moduleRole.getRoleId());
        List<ModuleRole> list = mrm.selectList(wrapper);
        List<Integer> ids=new ArrayList<>();
        for (ModuleRole mr: list
             ) {
            ids.add(mr.getModuleId());
        }
        return new ResultData(true,20000,"",ids,null);
    }

    /**
     *
     */
    @RequestMapping("/batchSave")
    public Object batchSave(@RequestBody SearchCon sc) {
        //先根据roleId把角色模块表中 下所有的数据全部删除
        QueryWrapper<ModuleRole> wrapper=new QueryWrapper<ModuleRole>();
        wrapper.eq("roleId", sc.getTypeName());
        List<ModuleRole> list1 = new ArrayList<ModuleRole>();
        List<ModuleRole> list = new ArrayList<ModuleRole>();
        if(sc.getNamelist().length==0){
            mrm.delete(wrapper);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("",true);
            return new ResultData(true,20000,"保存成功",map,0L);
        }else{
            mrm.delete(wrapper);
            for(String u: sc.getNamelist()) {
                ModuleRole rm=new ModuleRole();
                System.out.println(u);
                rm.setRoleId(sc.getTypeName());
                rm.setModuleId(Integer.valueOf(u));
                list.add(rm);
            }
            boolean r = mrm.modulerolelist(list);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("",r);
            return new ResultData(true,20000,"保存成功",map,0L);
        }
        //从模块id拼接的字符串中解析出每个模块Id,然后创建相关的对象
        //String[] ids=moduleId.split(",");
    }


}
